package com.hoelg.hgc.webservices;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.hoelg.hgc.DAO.HGCUser;
import com.hoelg.hgc.constants.Utils;

public class AddDesc extends WebServiceHGC {

	private static final String WS_NAME = "desc_C";

	public AddDesc(String catName, String descName) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		super(WS_NAME);
		try {
			this.webServiceParams.add(new BasicNameValuePair("u", HGCUser.getInstance().getLogin()));
			this.webServiceParams.add(new BasicNameValuePair("p", HGCUser.getInstance().getShaoneizedpwd()));
			this.webServiceParams.add(new BasicNameValuePair("cat", catName));
			this.webServiceParams.add(new BasicNameValuePair("desc", descName));
		} catch (Exception e) {
			Log.e("AddDesc_create", "Creation webService AddDesc, catched : " + e.toString());
		}

	}

	public Object executeWebService() {

		beforeExec();

		try {
			super.executeWebServiceBase();

			JSONArray jArray = Utils.parseJSON(this.result);

			if (jArray.length() >0) {
				if (jArray.length() == 1 ) {
					JSONObject json_data = jArray.getJSONObject(0);
					if(json_data.length() == 2){
						String status = json_data.getString("status");
						if (status.equals("OK")) {
							resultatWS = "OK";
						}
						else {
							String details = json_data.getString("details");
							Log.e("err_in_adding_desc","error : " + details);
						}
					}
				}
			}
			else{
				resultatWS = "emptyResult";
			}
		}
		catch(Exception e){
			Log.e("desc_add","erreur catchée dans " + WS_NAME + " : " + e.toString());
		}



		afterExec();

		return null;

	}

}
